package com.example.springboottest.student.controller;


import com.example.springboottest.example.annotation.aop.LogFilter;
import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.service.LockService;
import com.example.springboottest.student.service.StuPdfExportService;
import com.example.springboottest.student.service.StuService;
import com.example.springboottest.view.pdf.PdfExportService;
import com.example.springboottest.view.pdf.PdfView;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import java.awt.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("stu")
//@CrossOrigin(origins = "*")
public class StuController {
    @Autowired
//    @Qualifier("stuJdbcService")
    @Qualifier("stuMybatisService")
//    @Qualifier("stuJpaService")
    private StuService stuService;
    @Autowired
    private StuPdfExportService stuPdfExportService;
    @Autowired
    private LockService lockService;

    @LogFilter("查询学生---按ID查询学生")
    @ApiOperation(value = "按ID查询学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "String")
    })
    @RequestMapping(value = "/getById.api", method = {RequestMethod.POST, RequestMethod.GET})
    public Student getById(@RequestParam(value = "id") String id) {
        return stuService.doFindById(id);
    }

    @ApiOperation(value = "创建学生，默认创建成绩")
    @PostMapping(value = "/create.api")
    public void create(@RequestBody Student student) {
        stuService.doCreate(student);
    }

    @LogFilter("修改学生---保存请求日志")
    @ApiOperation(value = "修改学生")
    @PostMapping(value = "/update.api")
    public void update(@RequestBody Student student) {
        stuService.doUpdate(student);
    }

    @ApiOperation(value = "修改学生")
    @PostMapping(value = "/updateLock.api")
    public void updateLock(@RequestBody Student student) {
        stuService.doUpdateLock(student);
    }

    @LogFilter("查询所有学生---保存请求日志")
    @ApiOperation(value = "查询所有学生")
    @RequestMapping(value = "/findAll.api", method = {RequestMethod.POST, RequestMethod.GET})
    public List<Student> findAll() {
        return stuService.doFindALl();
    }


    @ApiOperation(value = "分页查询所有学生")
    @PostMapping(value = "/page.api")
    public Page<Student> page() {
        return stuService.doPage();
    }

    @ApiOperation(value = "按ID删除学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "String")
    })
    @RequestMapping(value = "/deleteById.api", method = {RequestMethod.POST, RequestMethod.GET})
    public void deleteById(@RequestParam(value = "id") String id) {
        stuService.doDeleteById(id);
    }

    @ApiOperation(value = "查询所有学生")
    @GetMapping(value = "/pdf.api")
    public ModelAndView pdf() {
        List<Student> list = stuService.doFindALl();
        // 定义pdf视图
        View view = new PdfView(stuPdfExportService);
        ModelAndView mv = new ModelAndView();
        // 设置视图
        mv.setView(view);
        // 加入数据模型
        mv.addObject("list", list);
        return mv;
    }

    @ApiOperation(value = "锁住资源")
    @RequestMapping(value = "/lock.api", method = {RequestMethod.POST, RequestMethod.GET})
    public String lock() {
        return stuService.doLock();
    }

    @ApiOperation(value = "测试分布式锁")
    @RequestMapping(value = "/distributeLock.api", method = {RequestMethod.POST, RequestMethod.GET})
    public String distributeLock(@RequestParam(value = "lockKey") String lockKey) {
        if (lockService.getDistributeLock(lockKey))
            return "获取到锁";
        else
            return "获取锁失败！！！";
    }

    @ApiOperation(value = "查看所有线程")
    @RequestMapping(value = "/watchThreads.api", method = {RequestMethod.POST, RequestMethod.GET})
    public String watchThreads() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
            Thread thread = stackTrace.getKey();
            StackTraceElement[] stack = stackTrace.getValue();
            if (thread.equals(Thread.currentThread())) {
                sb.append("\nCurrentThread Thread Name:\t" + thread.getName() + "\n");
            } else {
                sb.append("\nThread Name:\t" + thread.getName() + "\n");
            }
            for (StackTraceElement element : stack) {
                sb.append("\t" + element + "\n");
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }
}